package com.xm.xmapp.fragment;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.xm.xmapp.R;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.base.BaseFragment;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.ForGetPasswordInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.request.bean.Send_smsRequest;
import com.xm.xmapp.tools.StringTools;
import com.xm.xmapp.tools.TimeUtil;
import com.xm.xmapp.tools.TimeUtil.TimeListener;
import com.xm.xmapp.tools.Tools;

public class ForGetPasswordFragment1 extends BaseFragment implements
		OnClickListener {

	private ForGetPasswordInterFace in;
	private TextView next;
	private EditText phone, yanzhengma;
	private TextView getyanzhengma;
	private TimeUtil task;
	private int time;

	public ForGetPasswordFragment1(){
		
	}
	public ForGetPasswordFragment1(ForGetPasswordInterFace in) {
		super();
		this.in = in;
	}

	@Override
	public int setContentView() {
		
		return R.layout.fragment_forgetpassword1;
	}

	@Override
	public void initView() {
		
		next = (TextView) findViewById(R.id.next);
		phone = (EditText) findViewById(R.id.phone);
		yanzhengma = (EditText) findViewById(R.id.yanzhengma);
		getyanzhengma = (TextView) findViewById(R.id.getyanzhengma);
	}

	@Override
	public void initData() {
		
		time = 60;
	}

	@Override
	public void initEvent() {
		
		next.setOnClickListener(this);
		getyanzhengma.setOnClickListener(this);
		getyanzhengma.setClickable(true);
	}

	@Override
	public void onClick(View v) {
		
		switch (v.getId()) {
		case R.id.getyanzhengma:

			if (StringTools.isPhone(phone.getText().toString())) {
				Send_smsRequest requestBean = new Send_smsRequest();
				requestBean.phone = phone.getText().toString();
				requestBean.type = 2;
				promptDialog.show();
				MyAppcation.getMyAppcation().getPostData(getActivity(),
						requestBean, new GetDataInterFace<String>() {

							@Override
							public void onResponse(String response) {
								
								try {
									promptDialog.dismiss();
									getyanzhengma.setClickable(false);
									task = new TimeUtil((time + 1) * 1000, 1000);
									task.setTimeListener(new TimeListener() {

										@Override
										public void timeOnTick(long time0) {
										

											getyanzhengma.setText(time + "秒");
											time--;
										}

										@Override
										public void timeFinish() {
											
											time = 60;
											task.cancel();
											getyanzhengma.setClickable(true);
											getyanzhengma.setText("重新验证码");
											getyanzhengma.setBackgroundResource(R.drawable.myborder1);
										}
									});
									getyanzhengma.setBackgroundResource(R.drawable.myborder4);
									task.start();

								} catch (Exception e) {
									
								}
							}
						}, new DisssmissInterFace() {

							@Override
							public void putError(VolleyError error) {
								
								promptDialog.dismiss();
							}
						});
			} else {
				Tools.showMessage("请输入正确的手机号码");
			}
			break;

		case R.id.next:

			if (!StringTools.isPhone(phone.getText().toString())) {
				Tools.showMessage("请输入正确的手机号码");
			} else if (!StringTools
					.isLength(yanzhengma.getText().toString(), 4)) {
				Tools.showMessage("手机验证码为4到6位");
			} else if (in != null)
				in.getData(phone.getText().toString(), yanzhengma.getText()
						.toString());
			break;

		default:
			break;
		}
	}

}
